package wrap

import (
	"excel-handle/ent/enum"
	"excel-handle/ent/json"
	"excel-handle/util"
	"fmt"
	"io"
	"testing"
)

func TestConbination(t *testing.T) {

	// 读文件
	var dayInfos []json.DayInfo = readDays()
	var holiday json.Holiday = readHoliday()

	//var dayInfosFull []json.DayInfoFull
	infoFullsSliceOpt := make([]json.DayInfoFull, 0)

	// holiday转为slice
	holidaysSlice := make([]json.HolidayData, 0)
	for _, holidayData := range holiday.Holidays {
		holidaysSlice = append(holidaysSlice, holidayData)
	}

	// 处理dayinfo
	for _, dayInfo := range dayInfos {
		full := dayInfo.ToFull()
		// 状态描述转换填充
		full.StatusDesc = enum.DayStatus(full.Status).Desc()
		// 装入数组
		infoFullsSliceOpt = append(infoFullsSliceOpt, full)
		//dayInfosFull[idx] = full
	}

	dayInfos = nil

	// holidays 短路提升效率
	countMark := 0
	maxCount := len(holidaysSlice)

	infoFullsSliceOut := make([]json.DayInfoFull, 0)
	for _, full := range infoFullsSliceOpt {

		if full.Status == 2 || full.Status == 3 {
			println("stop!")
		}

		//for _, holidayInfo := range holidaysSlice {
		for i := countMark; i < maxCount; i++ {
			holidayInfo := holidaysSlice[countMark]

			// 短路本次循环
			if holidayInfo.Date != full.Date {
				continue
			}

			countMark++

			// 检测days与holiday的一致性
			if holidayInfo.IsOffDay == false {
				if full.Status != enum.WorkdayButWeekend.Val() {
					fmt.Errorf("holiday数据错误：%s", full.Date)
				}
			}

			// 填充
			full.StatusDesc = fmt.Sprintf("%s\r\n（%s）", full.StatusDesc, holidayInfo.Name)
		}
		println(util.ToJsonString(full))
		infoFullsSliceOut = append(infoFullsSliceOut, full)
	}

	// 输出至文件
	util.WriteJsonToFile(infoFullsSliceOut, util.OpenFileOrCreate(FullDaysPath))

}

func readDays() []json.DayInfo {

	bufReader := util.ToBufReader(DaysPath)
	data, _ := io.ReadAll(bufReader)
	//jsonObj := util.BytesToJson(data)

	var days []json.DayInfo
	util.JsonBytesToStruct(data, &days)

	return days
}

func readHoliday() json.Holiday {

	bufReader := util.ToBufReader(HolidayPath)
	data, _ := io.ReadAll(bufReader)
	//jsonObj := util.BytesToJson(data)
	var holiday json.Holiday
	// 传递的是引用，返回的还是该对象
	util.JsonBytesToStruct(data, &holiday)

	return holiday
}

func ReadFullDays() []json.DayInfoFull {

	bufReader := util.ToBufReader(FullDaysPath)
	data, _ := io.ReadAll(bufReader)

	var fullDays []json.DayInfoFull
	// 传递的是引用，返回的还是该对象
	util.JsonBytesToStruct(data, &fullDays)

	return fullDays
}
